//
//  ChangePasswordViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/15.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "ChangePasswordViewController.h"
typedef void (^surebtnResponse)(BOOL);

@interface ChangePasswordViewController ()
{
    UITextField *agotextField;
    UITextField *newtextField;
    UITextField *reNewtextField;
    UIButton *sureBtn;
    
}
@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"修改密码" fontSize:18];
    
    [self addLeftBackBtn:nil];

    
    [self makeUI];
    
    
    // Do any additional setup after loading the view from its nib.
}

-(void)makeUI{
    
//    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//    self.view = scrollView;
    
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    rootLayout.backgroundColor =UIColorFromRGB(0xf4f4f4);
    [rootLayout setTarget:self action:@selector(handleHideKeyboard:)];
        rootLayout.wrapContentHeight = NO;
    rootLayout.wrapContentWidth = NO;
    rootLayout.leftPadding=0;
    self.view =rootLayout;
    
    
    MyLinearLayout *uiview1 =[MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    
    uiview1.backgroundColor = [UIColor whiteColor];
    uiview1.myHeight=49;
    uiview1.myTopMargin=64;
    uiview1.widthDime.equalTo(rootLayout.widthDime);
    uiview1.bottomBorderLine = [[MyBorderLineDraw alloc] initWithColor:UIColorFromRGB(0xe5e5e5)]; //底部边界线如果为负数则外部缩进
    uiview1.bottomBorderLine.headIndent = 10;
    uiview1.bottomBorderLine.tailIndent = -10;
    [rootLayout addSubview:uiview1];

    
    //用户信息
  agotextField= [UITextField new];
    agotextField.borderStyle = UITextBorderStyleNone;
    agotextField.placeholder = @"请输入原始密码";
    agotextField.textAlignment = NSTextAlignmentLeft;
    agotextField.backgroundColor = [UIColor whiteColor];
    agotextField.secureTextEntry=NO;
    agotextField.font=[UIFont systemFontOfSize:14];
    

    //高度为40，左右间距为布局的10%, 顶部间距为剩余空间的10%
    agotextField.myHeight = 48;
    agotextField.widthDime.equalTo(uiview1.widthDime);
    agotextField.myTopMargin =0;
    agotextField.myLeftMargin=16;
    [uiview1 addSubview:agotextField];
 
    
    MyLinearLayout *uivie2 =[MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    
    uivie2.backgroundColor = [UIColor whiteColor];
    uivie2.myHeight=49;
    uivie2.myTopMargin=0;
    uivie2.widthDime.equalTo(rootLayout.widthDime);
    uivie2.bottomBorderLine = [[MyBorderLineDraw alloc] initWithColor:UIColorFromRGB(0xe5e5e5)]; //底部边界线如果为负数则外部缩进
    uivie2.bottomBorderLine.headIndent = 10;
    uivie2.bottomBorderLine.tailIndent = -10;
    [rootLayout addSubview:uivie2];

    //用户信息
   newtextField = [UITextField new];
    newtextField.borderStyle = UITextBorderStyleNone;
    newtextField.placeholder = @"请输入新密码";
    newtextField.textAlignment = NSTextAlignmentLeft;
    newtextField.backgroundColor = [UIColor whiteColor];
    newtextField.secureTextEntry=YES;
    newtextField.font=[UIFont systemFontOfSize:14];

    //高度为40，左右间距为布局的10%, 顶部间距为剩余空间的10%
    newtextField.myHeight = 48;
    newtextField.widthDime.equalTo(uivie2.widthDime);
    newtextField.myTopMargin =0;
    newtextField.myLeftMargin=16;
    [uivie2 addSubview:newtextField];

    
    MyLinearLayout *uivie3 =[MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    
    uivie3.backgroundColor = [UIColor whiteColor];
    uivie3.myHeight=49;
    uivie3.myTopMargin=0;
    uivie3.widthDime.equalTo(rootLayout.widthDime);
    uivie3.bottomBorderLine = [[MyBorderLineDraw alloc] initWithColor:UIColorFromRGB(0xe5e5e5)]; //底部边界线如果为负数则外部缩进
    uivie3.bottomBorderLine.headIndent = 10;
    uivie3.bottomBorderLine.tailIndent = -10;
    [rootLayout addSubview:uivie3];
    //用户信息
    reNewtextField = [UITextField new];
    reNewtextField.borderStyle = UITextBorderStyleNone;
    reNewtextField.placeholder = @"请再次输入新密码";
    reNewtextField.textAlignment = NSTextAlignmentLeft;
    reNewtextField.backgroundColor = [UIColor whiteColor];
    reNewtextField.secureTextEntry=YES;
    reNewtextField.font=[UIFont systemFontOfSize:14];

    //高度为40，左右间距为布局的10%, 顶部间距为剩余空间的10%
    reNewtextField.myHeight = 48;
    reNewtextField.widthDime.equalTo(uivie3.widthDime);
    reNewtextField.myTopMargin =0;
    reNewtextField.myLeftMargin=16;
    [uivie3 addSubview:reNewtextField];

    
    
    
    
    sureBtn=[UIButton new];
    sureBtn.myTopMargin=20;
    sureBtn.heightDime.equalTo(@(49));
    sureBtn.layer.cornerRadius=5;
    [sureBtn setTitle:@"确定修改" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBtn.myLeftMargin=sureBtn.myRightMargin=45*SHIJI_WIDTH;
    sureBtn.backgroundColor=UIColorFromRGB(0xfb5363);

    [rootLayout addSubview:sureBtn];
    
//    [agotextField.rac_textSignal  subscribeNext:^(id x){
//        NSLog(@"%@", x);
//    }];

    
    
    RACSignal *validAgoPasswordSignal =[agotextField.rac_textSignal map:^id(NSString * value) {
        
     return    @([self isValidPassword:value]);
        
    }];
    
    
    RACSignal *validNewPasswordSignal =[newtextField.rac_textSignal map:^id(NSString * value) {
        
        return    @([self isValidPassword:value]);
        
        
        
    }];
    
    RACSignal *validRePasswordSignal =[reNewtextField.rac_textSignal map:^id(NSString * value) {
        
        BOOL isSame=[reNewtextField.text isEqualToString:newtextField.text ]&&![reNewtextField.text isEqualToString:@""]&&![newtextField.text isEqualToString:@""]?YES:NO;
       
        return @(isSame);
        
        
        
    }];
    
    
//    RAC(agotextField, backgroundColor) =
//    [validAgoPasswordSignal
//     map:^id(NSNumber *passwordValid){
//         return[passwordValid boolValue] ? [UIColor clearColor]:[UIColor yellowColor];
//     }];
//    
//    RAC(newtextField, backgroundColor) =
//    [validNewPasswordSignal
//     map:^id(NSNumber *passwordValid){
//         return[passwordValid boolValue] ? [UIColor clearColor]:[UIColor yellowColor];
//     }];
//    

    

    
    RACSignal *sureBtnSignal=[RACSignal combineLatest:@[validAgoPasswordSignal,validNewPasswordSignal,validRePasswordSignal] reduce:^id (NSNumber*AgoPassword, NSNumber *NewPassword,NSNumber *RePassword){
        NSLog(@"%d-%d-%d",[AgoPassword intValue],[NewPassword intValue],[RePassword intValue]);
        
        
    return @([AgoPassword boolValue]&&[NewPassword boolValue]&&[RePassword boolValue]);

    }];
    
    [sureBtnSignal subscribeNext:^(NSNumber*signupActive) {
        sureBtn.enabled=[signupActive boolValue];
        sureBtn.backgroundColor=[signupActive boolValue]?UIColorFromRGB(0xfb5363):UIColorFromRGB(0xe5e5e5);

    }];
    
    [[[[sureBtn
        rac_signalForControlEvents:UIControlEventTouchUpInside]
       doNext:^(id x){
           
           sureBtn.enabled =NO;
       }]
      flattenMap:^id(id x){
          
          return[self signInSignal];
      }]
     subscribeNext:^(NSNumber*signedIn){
         sureBtn.enabled =YES;
         BOOL success =[signedIn boolValue];
         
         if(success){
             [self showSuccess:@"修改成功"];
             [self.navigationController popViewControllerAnimated:YES];
         }
         
     }];

    
    
    
}
- (RACSignal *)signInSignal {
    return [RACSignal createSignal:^RACDisposable *(id subscriber){
       
        [self setPasswordRequestDataWithResponseBlock:^(BOOL success) {
        
            [subscriber sendNext:@(success)];
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}
- (BOOL)isValidPassword:(NSString *)username {
    return username.length > 3;
}




-(void)setPasswordRequestDataWithResponseBlock:(surebtnResponse)completeBlock
{
    
    
    [self addProgressHUD];
    
    
    
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:[self getArtisanId], @"artisanId",agotextField.text,@"oldPassword",newtextField.text,@"newPassword",nil];
    
    
    NSLog(@"设置地址：%@",dictionary);
    [[WSNetworkApi shareManager]postWithUrl:@"/artisan/modifyPassword" parameters:dictionary option:RequestCenterCachePolicyNormal sucess:^(id responseObject) {
        [self removeStatuslable];
        
        NSLog(@"%@",responseObject);
        
        
        
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
           
            completeBlock(YES);
            
            
        }
        else{
            completeBlock(NO);

            [self showFailed:responseObject[@"message"]];
            
        }
        
        
    } failur:^(NSError *error) {
        [self removeStatuslable];
        
    }];
    
}

-(void)handleHideKeyboard:(id)sender
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
